{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f24edfa0",
   "metadata": {},
   "source": [
    "这里的偏函数和数学意义上的偏函数不同"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "738eabd4",
   "metadata": {},
   "source": [
    "我们可以通过设定参数的默认值，降低函数调用的难度，而偏函数也能做到这点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d896240",
   "metadata": {},
   "source": [
    "int函数提供额外base参数，可以做N进制转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cfa5c95a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int('13')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "36c3ce25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int('11', base=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f51b175",
   "metadata": {},
   "source": [
    "如果要转换大量的二进制字符串，每次都传入int(x, base=2)会非常麻烦。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2778f119",
   "metadata": {},
   "source": [
    "可以定义一个int2，默认把base=2传入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "471cc05a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def int2(x,base=2):\n",
    "    return int(x,base)\n",
    "int2('11')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2452a79b",
   "metadata": {},
   "source": [
    "functools.partial就是帮助我们创建一个偏函数，不需要我们自己定义int2()，具体如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4efc87dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import functools\n",
    "int2 = functools.partial(int,base=2)\n",
    "int2('11')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41d4d8ae",
   "metadata": {},
   "source": [
    "functools.partial的作用就是把一个函数的某些参数给固定住，也就是设置默认值，返回一个新函数，调用这个函数会更简单"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a2e87e5",
   "metadata": {},
   "source": [
    "创建偏函数时，实际上可以接收函数对象，*args,**kw这3个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5d3d45c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int2 = functools.partial(int,base=2)\n",
    "int2('11')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1f5cb502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#相当于\n",
    "kw = {'base': 2}\n",
    "int('11', **kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7b05a25c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max2 = functools.partial(max,10)\n",
    "max2(2,5,7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ac7ae2bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#相当于\n",
    "args = (10,2,5,7)\n",
    "max(*args)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32bf634e",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9c470e2",
   "metadata": {},
   "source": [
    "当函数的参数个数太多，需要简化时，使用functools.partial可以创建一个新函数，这个新函数可以固定原函数的部分参数，从而再调用时更简单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ae6f96a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
